package com.example.controller;

import com.example.bean.UserInfoBean;
import com.example.service.IUserService;
import com.example.utils.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/rest")
public class UserRestController {
    // 使用ioc、di进行依赖注入
    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    public ResponseEntity<UserInfoBean> getUserById(@PathVariable("id") Integer userId) {
        UserInfoBean userInfoBean = userService.getUserById(userId);
        return new ResponseEntity<UserInfoBean>(userInfoBean, HttpStatus.OK);
    }

    @RequestMapping(value = "/user/", method = RequestMethod.GET)
    public ResponseEntity<List<UserInfoBean>> getAllUser() {
        List<UserInfoBean> userInfoBeanList = userService.queryUserInfoByParams(new UserInfoBean());
        return new ResponseEntity<List<UserInfoBean>>(userInfoBeanList, HttpStatus.OK);
    }

    @RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
    public ResponseEntity<String> updateUserById(UserInfoBean userInfoBean, @PathVariable("id") Integer userId) {
        // 将put请求中的id值，设置给从 request 中获取的实体bean
        userInfoBean.setUserid(userId);
        // 调用 service 层进行修改操作
        int i = userService.updateUserById(userInfoBean);
        return new ResponseEntity<String>(i+"", HttpStatus.OK);
    }

}
